package org.meetunexpectedly.controller;

import org.meetunexpectedly.entity.video.Type;
import org.meetunexpectedly.entity.video.Video;
import org.meetunexpectedly.entity.vo.BasePage;
import org.meetunexpectedly.entity.vo.Result;
import org.meetunexpectedly.service.user.UserService;
import org.meetunexpectedly.service.video.TypeService;
import org.meetunexpectedly.service.video.VideoService;
import org.meetunexpectedly.util.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Description: 主页控制类
 * @Author: pupil
 * @Date: 2024/08/11 下午 6:40
 */
@RestController
@RequestMapping("/meetunexpectedly/index")
public class IndexController {

    @Autowired
    private UserService userService;

    @Autowired
    private VideoService videoService;

    @Autowired
    private TypeService typeService;

    /**
     * 获取用户搜索记录
     *
     * @param request 网络请求
     * @return
     */
    @GetMapping("/search/history")
    public Result searchHistory(HttpServletRequest request) {
        return Result.ok().data(userService.searchHistory(JwtUtils.getUserId(request)));
    }

    /**
     * 删除用户搜索记录
     *
     * @param request
     * @return
     */
    @DeleteMapping("/search/history")
    public Result deleteHistory(HttpServletRequest request) {
        if (!userService.deleteSearchHistory(JwtUtils.getUserId(request))) {
            return Result.error().message("搜索记录删除失败");
        }
        return Result.ok().message("搜索记录删除成功");
    }

    /**
     * 根据id获取视频详情
     *
     * @param videoId 视频id
     * @param request 网络请求
     * @return
     */
    @GetMapping("/video/{videoId}")
    public Result getVideoById(@PathVariable Long videoId, HttpServletRequest request) {
        Long userId = JwtUtils.getUserId(request);
        return Result.ok().data(videoService.getVideoById(videoId, userId));
    }

    /**
     * 根据用户id获取视频
     *
     * @param userId   用户id
     * @param basePage 基础分页信息
     * @param request  网络请求
     * @return
     */
    @GetMapping("/video/user")
    public Result getVideoByUserId(@RequestParam(required = false) Long userId, BasePage basePage, HttpServletRequest request) {
        userId = userId == null ? JwtUtils.getUserId(request) : userId;
        return Result.ok().data(videoService.getVideoByUserId(userId,basePage));
    }

    /**
     * 搜索视频
     * @param searchName 搜索参数,可能是标题,用户,YV
     * @param basePage 基础分页信息
     * @param request 网络请求
     * @return
     */
    @GetMapping("/search")
    public Result searchVideo(@RequestParam(required = false) String searchName,BasePage basePage,HttpServletRequest request){
        Long userId = JwtUtils.getUserId(request);
        return Result.ok().data(videoService.searchVideo(searchName,basePage,userId));
    }

    /**
     * 兴趣推送视频,以流的形式,返回数据多少不重要
     * 前端根据阈值进行请求该接口
     * @param request
     * @return
     */
    @GetMapping("/pushVideos")
    public Result pushVideos(HttpServletRequest request){
        Long userId = JwtUtils.getUserId(request);
        return Result.ok().data(videoService.pushVideos(userId));
    }


    /**
     * 获取热度排行榜
     * @return
     */
    @GetMapping("/video/hot/rank")
    public Result getHotRank(){
        return Result.ok().data(videoService.getHotRank());
    }

    /**
     * 推送热门视频
     * @return
     */
    @GetMapping("/video/hot")
    public Result getHotVideos(){
        return Result.ok().data(videoService.getHotVideos());
    }

    /**
     * 根据视频标签推送相似视频
     * @param video 视频信息 包含：labelNames 标签，多个标签以，分割，可以直接取视频中的label； videoId  视频id
     * @return
     */
    @GetMapping("/video/similar")
    public Result pushSimilarVideos(Video video){
        return Result.ok().data(videoService.pushSimilarVideos(video));
    }

    /**
     * 获取所有分类
     * @param request 网络请求
     * @return
     */
    @GetMapping("/types")
    public Result getTypes(HttpServletRequest request){
        List<Type> types = typeService.getTypes(request);
        return Result.ok().data(types);
    }

    /**
     * 根据视频分类获取视频
     * @param typeId 分类id
     * @return
     */
    @GetMapping("/video/type/{typeId}")
    public Result getVideoByTypeId(@PathVariable Long typeId){
        return Result.ok().data(videoService.getVideoByTypeId(typeId));
    }
}